<?php
if(!defined('IN_THANKPHP_SYSTEM')) exit('Access Denied!');

abstract class view extends base {
	private static $only = true;
	private $script = array();
	private $css = array();
	private $vo;
	private	$html = array(
				'type'		=> 'html',
				'charset'	=> 'utf-8',
				'tpl'		=>	null,
				'css'		=>	null,
				'script'	=>	null
			);
			
	public function dispatcher(){
		global $DEBUG;
		if($DEBUG)
		print 'view dispatch begin......<br/>';
	}
	
	abstract function page();	
	public function __construct(){
		parent::__construct();
		$this->vo = new stdClass;
		if(self::$only){
		}else{
			self::$only = false;
		}
	}
	
	public function show(){
		$this->header();
		$this->get_tpl();
		$this->html();
	}
	
	private function html(){
		global $REL_PATH;
		if($this->html['tpl'] == TPL_DEFAULT){
			$name = 'default';
		}else{
			$name = substr($REL_PATH, 0, -4);
		}
		$filename = $this->save($name);
		include_once TPL_CACHE.DIR_S.$filename;
	}
	
	private function save($name){
		include_once TPL_FILE;
		$fileinfo = isset($TPL[$name])?$TPL[$name]:false;
		if($fileinfo){
			$ctime = $fileinfo['ctime'];
			$file = TPL_CACHE.DIR_S.$fileinfo['cname'].'.tpl.php';
			if($ctime - time() < EXPIRE_TIME || file_exists($file)){
				return $cname;
			}
		}
		$cname = $this->get_cname($name).'.tpl.php';
		$file = TPL_CACHE.DIR_S.$cname;
		$r = array(
			'ctime' => time(),
			'cname'	=> $cname
		);
		$TPL[$name] = $r;
		$data  = "<?php\r\nif(!defined('IN_SYSTEM')) exit('Access Denied!');\r\n";
		$data .= "\$TPL = ".var_export($TPL, TRUE).";\r\n";
		file_put_contents($file, $data); //print_r($name);exit(__LINE__);
		$data = $this->parse(TPL_ROOT.DIR_S.$name.'.tpl.php');//print $data;
		file_put_contents($file, $data);
		return $cname;
	}
	
	private function parse($file){
		//html模板解析
		return file_get_contents($file);
	}
	
	private function get_cname($name){
		//模板缓存名
		return md5($name);
	}
	
	public function set_tpl($t, $dir = null){
		if($dir) $dir .= DIR_S;
		$path = TPL_ROOT.DIR_S.$dir.$t.'.tpl.php';
		if(!file_exists($t)){
			$this->html['tpl'] = TPL_DEFAULT;
		}else{
			$this->html['tpl'] = $path;
		}
		return;
	}
	
	public function get_tpl(){
		if(!$this->html['tpl']){
			$this->search_tpl();
		}
		return $this->html['tpl'];
	}
	
	public function searchTpl(){
		global $REL_PATH;
		$path = TPL_ROOT.DIR_S.substr($REL_PATH, 0, -4).'.tpl.php';
		if(!file_exists($path)){
			$this->html['tpl'] = TPL_DEFAULT;
		}else{
			$this->html['tpl'] = $path;
		}
		return;
	}
	

	
	public function header(){
		switch($this->html['type']){
			case 'css':{
				break;
			}
			case 'js':{
				header("Content-type: text/javascript; charset=".$this->html['charset']);
				break;
			}
			case 'images':{
				break;
			}
			case 'html':{
				header("Content-type: text/html; charset=".$this->html['charset']);
				break;
			}
			default :{
				header("Content-type: text/html; charset=".$this->html['charset']);
				break;
			}
		}
		return $this;
	}
	
	public function addCss($s){
		$this->css[] = $s;
	}
	
	public function addVar($name, $value = null){
		if(!$value){
			global ${$name};
			$value = isset(${$name})?(${$name}):false;
		}
		$this->vo->$name = $value;
		return $this;
	}

	public function addScript($s){
		$this->script[] = $s;
	}
	
	private function sendCss(){
		$str = null;
		foreach($this->css AS $v){
			$str .= '<link href="'.URL_ROOT.$v.'" rel="stylesheet" type="text/css" />\n';
		}
		return $this->html['css'] = $str;
	}
	
	public function setHeader($h){
		$this->html['type'] = $h;
		return $this;
	}
	
	private function sendScript(){
		$str = null;
		foreach($this->script AS $v){
			$str .= '<script language="JavaScript" src="'.URL_ROOT.$v.'"></script>\n';
		}
		return $this->html['script'] = $str;
	}
}